package com.example.result.kotlin.base

import android.util.ArraySet
import android.util.SparseArray
import android.util.SparseIntArray
import androidx.core.text.isDigitsOnly
import com.example.result.alogrithm.*
import kotlinx.coroutines.internal.ThreadSafeHeap
import org.junit.Test
import java.lang.StringBuilder
import java.lang.reflect.Array
import java.math.BigInteger
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.LinkedHashMap
import kotlin.math.abs

class ListTest {
    @Test
    fun test_sort(){

       var list=  mutableListOf<Int>()
        for(i in 0..100){
            list.add(i)
            list.clear()
            list.reversed()
        }
        list.sort()
        list.toList()
        assert(list[0]==0)
        list.sortDescending()
        list.sortByDescending { it }
        assert(list[0]==100)
        var strs=list.map { it -> it.toString() }
       assert( strs.sorted().run { this[0]=="0"&&this[size-1]=="99" } )

    }
    @Test
    fun test_heap(){
        val heap = PriorityQueue<Int>(compareByDescending { it })
        heap.offer(1)
        heap.offer(99)
        heap.offer(88)
        heap.offer(100)
        assert(heap.peek()==100)
        assert(heap.poll()==100)
        assert(heap.poll()==99)
        val heap2=PriorityQueue<Int>(compareBy{it})
        heap2.add(100)
        heap2.add(0)
        heap2.add(88)
        assert(heap2.poll()==0)

    }
    data class Item(var a:Int):Comparable<Item>{
        override fun hashCode(): Int {
            return super.hashCode()
        }

        override fun compareTo(other: Item): Int {
            return 1
        }
    }
    @Test
    fun testStack(){
        val stack = LinkedList<Int>()
        stack.push(1)
        stack.push(2)
        stack.push(3)
        assert(stack.peek()==3)
        assert(stack.pop()==3)
        assert(stack.pop()==2)
    }

    @Test
    fun  test_queue() {
     val queue=LinkedList<Int>()
        queue.offer(1)
        queue.offer(2)
        queue.offer(3)
        assert(queue.peek()==1)
        assert(queue.poll()==1)
        assert(queue.poll()==2)
    }
    @Test
    fun test_graph(){
        var g=Graph(6)
        g.addEdge(0,1)
        g.addEdge(0,2)
        g.addEdge(2,3)
        g.addEdge(2,4)
        g.addEdge(3,4)
        g.addEdge(3,5)
        g.addEdge(0,5)

        val depthFirstPaths = DepthFirstPaths(g, 0)
        depthFirstPaths.path(5)?.apply {
            while(size>0){
                print("${pop()} -> ")
            }
            println()
        }
        BreadthFirstPaths(g,0).path(5)?.apply {
            while(size>0) {
                print("${pop()} -> ")
            }
            println()
        }
        var a=Array<ArrayList<Int>>(2){
            ArrayList<Int>()
        }
        ArrayList<Int>().toList().sorted()
        abs(-1)

    }
    @Test
    fun test_insertSort(){
//        var arr= intArrayOf(3,2,4,5,1,9,7)
        var arr= Random().ints(1000000).toArray()
        var sort=InsertSort()
        var time=sort.timeOfSort(arr,0,arr.size-1)
        println("time:${time}")
      /*  assert(sort.isSorted(arr,0,arr.size-1))
        for(item in arr){
            print("$item ->")
        }
        println()*/
    }

    @Test
    fun test_linkHashMap(){
        var lru=object:LinkedHashMap<Int,Int>(3,0.7f,true){
            override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Int, Int>?): Boolean {
                return super.removeEldestEntry(eldest)
            }
        }
        lru.put(1,2)
        lru.get(1)
        lru.keys.iterator()
    }
    @Test
    fun test_quickSort(){
        var arr= Random().ints(1000000).toArray()

        var sort=QuickSort()
        var time=sort.timeOfSort(arr,0,arr.size-1)
        println("time:${time}")
        /*var i=0
        for(item in arr){
            print("$item -> ")
            i++
            if(i%10==0){
                println()
            }
        }
        println()*/
        assert(sort.isSorted(arr,0,arr.size-1))
    }
    @Test
    fun test_mergeSort(){
        var arr= Random().ints(1000000).toArray()
        var sort=MergeSort()
        var time=sort.timeOfSort(arr,0,arr.size-1)
        println("time:${time}")
        /*var i=0
        for(item in arr){
            print("$item -> ")
            i++
            if(i%10==0){
                println()
            }
        }
        println()*/
        assert(sort.isSorted(arr,0,arr.size-1))
    }

}